package com.polaris.live.adapter

import android.view.View
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.databinding.AdapterAreaCodeBinding
import com.polaris.live.resp.back_resp.AreaCodeResp
import com.polaris.live.translation.R.string
import com.polaris.live.widget.country.countryIcons

/**
 * 区号的Adapter
 *
 * @author Created by 半仙 on 2023/8/15/015 17:13
 */
class AreaCodeAdapter : BaseBindingAdapter<AdapterAreaCodeBinding, AreaCodeResp>() {

    private var isSearch = false

    override fun convert(holder: VBViewHolder<AdapterAreaCodeBinding>, item: AreaCodeResp) {
        holder.vb.apply {
            if (item.areaCode != null) {
                ivCountryIcon.visibility = View.VISIBLE
                ivCountryIcon.setImageResource(item.countryCode?.let { countryIcons[it] }
                    ?: R.color.gray)
            } else {
                ivCountryIcon.visibility = View.GONE
            }
            areaCountry.text = item.countryName
            areaCode.text = if (item.areaCode != null) "+${item.areaCode}" else ""

            letter.text = item.firstLetter

            if (!isSearch) {
                if (holder.layoutPosition == 0) {
                    letter.visibility = View.VISIBLE
                    letter.text = StringUtils.getString(string.ipcountry)
                } else if (holder.layoutPosition == 1) {
                    letter.visibility = View.VISIBLE
                } else {
                    if (item.firstLetter != data[holder.layoutPosition - 1].firstLetter) {
                        letter.visibility = View.VISIBLE
                    } else {
                        letter.visibility = View.GONE
                    }
                }
            } else {
                if (holder.layoutPosition == 0) {
                    letter.visibility = View.VISIBLE
                } else {
                    if (item.firstLetter != data[holder.layoutPosition - 1].firstLetter) {
                        letter.visibility = View.VISIBLE
                    } else {
                        letter.visibility = View.GONE
                    }
                }
            }
        }
    }

    fun setIsSearch(isSearch: Boolean) {
        this.isSearch = isSearch
    }

}

